package book.controller;

import book.entity.Model;
import book.entity.chushou;
import book.entity.cs_dingdan;
import book.entity.tushu;
import book.service.CS_DingDanService;
import book.service.ChuShouService;
import book.service.TushuServices;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("tuihui")
public class TuiHuiContrller {

    @Autowired
    ChuShouService chushou;

    @Autowired
    CS_DingDanService dingdan;

    @Autowired
    TushuServices tushu;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        return "chushou/tuihui";
    }


    @RequestMapping(value = "all",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cha(@RequestParam("offset") int offset,@RequestParam("limit") int limit,chushou condition){
        List<chushou> list=chushou.getAll(offset,limit,condition);
        Map<String, Object> map = new HashMap<String,Object>();
        int total = chushou.getCount();
        map.put("total",total);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "getById",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getById(@RequestParam("id") String id){
        Map<String,Object> map=new HashMap<String,Object>();
        chushou list= chushou.getById(id);
        map.put("total",list.getCsdingdan().size());
        map.put("rows",list.getCsdingdan());
        return map;
    }

    @RequestMapping(value = "getTSId",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> getId(@RequestParam("id") int id){
        Map<String,Object> map=new HashMap<String,Object>();
        tushu list= tushu.getById(id);
        map.put("rows",list);
        return map;
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String toadd(){
        return "chushou/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> add(chushou dz){
        System.err.println(JSON.toJSONString(dz));
        Map<String, Object> map = new HashMap<String,Object>();
        dz.setCs_id(getriqi());
        chushou.insert(dz);
        for(int i=0;i<dz.getList().size();i++){
            if(dz.getList().get(i) != null){
                cs_dingdan dd=new cs_dingdan();
                book.entity.tushu t=new tushu();
                t.setTs_id(dz.getList().get(i));
                dd.setCs_id(dz);
                dd.setTs_id(t);
                dingdan.insert(dd);
            }
        }
        map.put("result",true);
        return map;
    }

    public String getriqi(){
        String a = "DH";
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        a += df.format(day);
        return a;
    }


    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delete(@RequestParam("id") String id){
        Map<String, Object> map = new HashMap<String,Object>();
        dingdan.delete(id);
        chushou.delete(id);
        map.put("result",true);
        return map;
    }


    @RequestMapping(value = "/batchDel",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> todelete(Model model){
        System.err.println(JSON.toJSONString(model));
        Map<String, Object> map = new HashMap<String,Object>();
        dingdan.todel(model.getIds());
        chushou.todel(model.getIds());
        map.put("result",true);
        return map;
    }


//    @RequestMapping(value = "/update",method = RequestMethod.GET)
//    public String update(@RequestParam("id") int id,HttpServletRequest request){
//        duzhe d=duzheServices.getById(id);
//        List<Dz_ShenFen> shenfen = dz_shenFen.getAll();
//        request.setAttribute("duixiang",d);
//        request.setAttribute("list",shenfen);
//        return "user/update";
//    }
//
//    @RequestMapping(value = "/update",method = RequestMethod.POST)
//    @ResponseBody
//    public Map<String,Object> toupdate(duzhe dz){
//        Map<String, Object> map = new HashMap<String,Object>();
//        duzheServices.update(dz);
//        map.put("result",true);
//        return map;
//    }
}
